package com.apes.fa.model;


import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FaCardImport.CONST_TABLE_NAME)
public class FaCardImport extends Model {

    public static final String CONST_TABLE_NAME = "fa_cart_imp";

    @Id
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "批量导入资产", prefix = "%(company)", padding = 5)
    @Comment("单号")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;


    @Column(name = "state")
    @Comment("状态")
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUid;

    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @OneToMany(mappedBy = "faCardImport", cascade =  CascadeType.ALL, orphanRemoval = true)
    private List<FaCardImportItem> faCardImpItems = new ArrayList<>();

    public List<FaCardImportItem> getFaCardImpItems() {
        for (FaCardImportItem faCardImpItem : faCardImpItems) {
            faCardImpItem.setFaCardImport(this);
        }
        return faCardImpItems;
    }
}
